<?php
namespace Kxdr\Controller;
use Think\Controller;
class EventController extends ExtendController {
		public function remove_event(){
			$eid = I("eid");
			$EV = M("KxdrEvents");
			$re = $EV->where(Array("sid"=>$this->sid,"id"=>$eid))->save(Array("status"=>9));
			if(!$re){
				$reda["hasError"]=true;
				$reda["msg"]="删除失败！";
			}else{
				$reda["hasError"]=false;
				$reda["msg"]="删除成功！";
			}
			$this->ajaxReturn($reda);
		}
		public function add_result(){
			$save = I("save");
			
			$game_id = I("game_id");
			$GAME = M("KxdrGames");
			$game = current($GAME->where(Array("id"=>$game_id))->getField("`id`,`fid`, `league_type`, `outside_id`, `match_id`, `short_name`, `name`,`start_time`, `edit_time`, `is_pub`, `tag`, `game_category`"));
			// print_r($game);
			$this->assign("game",$game);
				
			if($game["game_category"]=="football"){
				$SCORE = M("KxdrGamesSoccerTeamscore");
				if($save && !$game["is_pub"]){
					$score_id=I("score_id");
					$score["home_flag"]=I("home_flag");
					$score["guest_flag"]=I("guest_flag");
					$score["home_team_score"]=I("home_team_score");
					$score["guest_team_score"]=I("guest_team_score");
					$score["home_team_redcard"]=I("home_team_redcard");
					$score["guest_team_redcard"]=I("guest_team_redcard");
					$score["home_team_yellowcard"]=I("home_team_yellowcard");
					$score["guest_team_yellowcard"]=I("guest_team_yellowcard");
					$score["home_team_halfscore"]=I("home_team_halfscore");
					$score["guest_team_halfscore"]=I("guest_team_halfscore");
					$score["home_shoot"]=I("home_shoot");
					$score["home_target_shoot"]=I("home_target_shoot");
					$score["home_woodwork_shoot"]=I("home_woodwork_shoot");
					$score["home_straight_plug_ball"]=I("home_straight_plug_ball");
					$score["home_offside"]=I("home_offside");
					$score["home_steals"]=I("home_steals");
					$score["home_free_kick"]=I("home_free_kick");
					$score["home_foul"]=I("home_foul");
					$score["home_corner"]=I("home_corner");
					$score["home_foul_ball"]=I("home_foul_ball");
					$score["home_over_25_yard_passes"]=I("home_over_25_yard_passes");
					$score["home_pass_success_rate"]=I("home_pass_success_rate");
					$score["home_pass_center_success_rate"]=I("home_pass_center_success_rate");
					$score["home_steal_success_rate"]=I("home_steal_success_rate");
					$score["home_header_success_rate"]=I("home_header_success_rate");
					$score["home_ball_control_rate"]=I("home_ball_control_rate");
					$score["guest_shoot"]=I("guest_shoot");
					$score["guest_target_shoot"]=I("guest_target_shoot");
					$score["guest_woodwork_shoot"]=I("guest_woodwork_shoot");
					$score["guest_straight_plug_ball"]=I("guest_straight_plug_ball");
					$score["guest_offside"]=I("guest_offside");
					$score["guest_steals"]=I("guest_steals");
					$score["guest_free_kick"]=I("guest_free_kick");
					$score["guest_foul"]=I("guest_foul");
					$score["guest_corner"]=I("guest_corner");
					$score["guest_foul_ball"]=I("guest_foul_ball");
					$score["guest_over_25_yard_passes"]=I("guest_over_25_yard_passes");
					$score["guest_pass_success_rate"]=I("guest_pass_success_rate")*100;
					$score["guest_pass_center_success_rate"]=I("guest_pass_center_success_rate")*100;
					$score["guest_steal_success_rate"]=I("guest_steal_success_rate")*100;
					$score["guest_header_success_rate"]=I("guest_header_success_rate")*100;
					$score["guest_ball_control_rate"]=I("guest_ball_control_rate")*100;
					$re = $SCORE->where(Array("sid"=>$this->sid,"id"=>$score_id))->save($score);
					if(!$re){
						if($re===false){
							$message = "更新失败！请重试";
						}else{
							$message = "您没有修改任何数据。";
						}
					}
				}else{
					$sw["game_id"]=$game_id;
					$score = current($SCORE->where($sw)->getField("`id`, `private_home_team_id`, `private_guest_team_id`, `home_team`, `guest_team`, `gaming_time`, `home_flag`, `guest_flag`,  `home_team_score`, `guest_team_score`, `home_team_redcard`, `guest_team_redcard`, `home_team_yellowcard`, `guest_team_yellowcard`, `home_team_halfscore`, `guest_team_halfscore`, `home_shoot`, `home_target_shoot`, `home_woodwork_shoot`, `home_straight_plug_ball`, `home_offside`, `home_steals`, `home_free_kick`, `home_foul`, `home_corner`, `home_foul_ball`, `home_over_25_yard_passes`, `home_pass_success_rate`, `home_pass_center_success_rate`, `home_steal_success_rate`, `home_header_success_rate`, `home_ball_control_rate`, `guest_shoot`, `guest_target_shoot`, `guest_woodwork_shoot`, `guest_straight_plug_ball`, `guest_offside`, `guest_steals`, `guest_free_kick`, `guest_foul`, `guest_corner`, `guest_foul_ball`, `guest_over_25_yard_passes`, `guest_pass_success_rate`, `guest_pass_center_success_rate`, `guest_steal_success_rate`, `guest_header_success_rate`, `guest_ball_control_rate`"));
				}
				$this->assign("message",$message);
				$this->assign("game_id",$game_id);
				$this->assign("score",$score);
				$this->display("add_result_".$game["game_category"]);
				die();
			}
			$this->display("add_result_not_support");
		}
		
		public function user(){
			Vendor("Page");
			$eid = I("eid");
			$UG = D("EventGuessUserView");
			$whug["sid"]=$this->sid;
			$whug["eid"]=$eid;
			$UG->wharr=$whug;
			
			$total = $UG->getTotal();
			$Page = new \Page($total,20);
			$show = $Page->show();
			
			$UG->lmtstr=$Page->firstRow.','.$Page->listRows;
			$user_guess = $UG->getEventGuessUser();

			$this->assign("show",$show);
			$this->assign("eid",$eid);
			$this->assign("user_guess",$user_guess);
			$this->display();
		}
		public function user_gifts(){
			Vendor("Page");
			$eid = I("eid");
			$gift_type = I("gift_type");
			$UG = D("EventUserGiftView");
			$whug["sid"]=$this->sid;
			$whug["eid"]=$eid;
			if($gift_type){
				$GTP = C("GFT_TYPE");
				$whug["gift_type"]=$GTP[$gift_type]["id"];
			}
			$UG->wharr=$whug;
			
			$total = $UG->getTotal();
			$Page = new \Page($total,20);
			$show = $Page->show();
			
			$UG->lmtstr=$Page->firstRow.','.$Page->listRows;
			$user_guess = $UG->getEventGuessUser();

			$WL = S("WL");
			if(!$WL){
				Vendor("taobaosdk.top.request.LogisticsCompaniesGetRequest");
				$req = new \LogisticsCompaniesGetRequest;
				$req->setFields("id,code,name");
				$req->setIsRecommended("true");
				// $req->setOrderMode("offline");
				$WL = $this->c->execute($req);
			}
			foreach($WL->logistics_companies->logistics_company as $cmn){
				$wl[]=(array)$cmn;
			}
			
			$this->assign("wl",$wl);
			$this->assign("gift_type",$gift_type);
			$this->assign("show",$show);
			$this->assign("eid",$eid);
			$this->assign("user_guess",$user_guess);
			$this->display();
		}
		public function item_send_log(){
			Vendor("Page");
			$eid = I("eid");
			$LOG = D("EventSendLogView");
			$LOG->wharr["eid"]=$eid;
			
			$total = $LOG->getTotal();
			$Page = new \Page($total,20);
			$show = $Page->show();
			
			$LOG->lmtstr=$Page->firstRow.','.$Page->listRows;
			$log= $LOG->getEventSendLog();

			$this->assign("eid",$eid);
			$this->assign("show",$show);
			$this->assign("log",$log);
			$this->display();
		}
		public function get_user_addr(){
			$ugid = I("ugid");
			$uid = I("uid");
			$ADD = M("UserAddress");
			$whst["sid"]=$this->sid;
			$whst["kxdr_uid"]=$uid;
			$whst["is_def"]=1;
			$address = current($ADD->where($whst)->getField("`id`, `nationality`, `province`, `city`, `city_part`, `address`, `name`, `tel`"));
			if($address){
				$re["hasError"] = false;
				$re["address"] = $address;
			}else{
				$re["hasError"] = true;
			}
			$re["wl"]["wlcode"]="";
			$this->ajaxReturn($re);
		}
		public function send_gift(){
			$wlmc = I("wlmc");
			$wlid = I("wlid");
			$wlcode = I("wlcode");
			$ugid = I("ugid");
			$uid = I("uid");
			$province = I("province");
			$city = I("city");
			$city_part = I("city_part");
			$addr = I("addr");
			$name = I("name");
			$tel = I("tel");
			$ac = I("ac");
			$sendid = I("sendid");
			
			
			$data["sid"]=$this->sid;
			$data["kxdr_uid"]=$uid;
			// // $data["shop_uid"]=
			// // $data["sys_uid"]=
			$data["ugid"]=$ugid;
			
			$SEND = M("GiftsSendlog");
			if($ac=="add"){
				$has = $SEND->where($data)->getField("id");
				if($has){
					$this->ajaxReturn(Array("hasError"=>true));
					die();
				}
			}
			
			// $data["egid"]=
			// $data["user_nick"]=
			// $data["gift_id"]=
			$data["send_time"]=date("Y-m-d H:i:s");
			$data["is_auto_send"]=0;
			$data["sender_nick"]=session("VISITOR_NICK");
			$data["province"]=$province;
			$data["city"]=$city;
			$data["city_part"]=$city_part;
			$data["address"]=$addr;
			$data["true_name"]=$name;
			$data["mobile"]=$tel;
			$data["wlname"]=$wlmc;
			$data["wlid"]=$wlid;
			$data["wlcode"]=$wlcode;
			$data["version"]="kxdr";
			
			if($ac=="add"){
				$re = $SEND->add($data);
			}else{
				$data["id"]=$sendid;
				$re = $SEND->save($data);
			}
			
			$this->ajaxReturn(Array("id"=>$re));
		}
		public function get_send_log(){
			$ugid = I("ugid");
			$uid = I("uid");
			$ADD = M("UserAddress");
			$whst["sid"]=$this->sid;
			$whst["kxdr_uid"]=$uid;
			$whst["is_def"]=1;
			$address = current($ADD->where($whst)->getField("`id`, `nationality`, `province`, `city`, `city_part`, `address`, `name`, `tel`"));
			
			$data["sid"]=$this->sid;
			$data["kxdr_uid"]=$uid;
			$data["ugid"]=$ugid;
			$SEND = M("GiftsSendlog");
			$wl = current($SEND->where($data)->getField("id,wlid,wlcode"));
			
			if($address){
				$re["hasError"] = false;
				$re["address"] = $address;
			}else{
				$re["hasError"] = true;
			}
			
			$re["wl"]=$wl;
			$this->ajaxReturn($re);
		}
		public function send_log_download(){
			$eid = I("eid");
			$LOG = D("EventSendLogView");
			$LOG->wharr["eid"]=$eid;
			$LOG->send_log_download();
		}
		public function download_giftlist(){
			$eid = I("eid");
			$gift_type = I("gift_type");
			$UG = D("EventUserGiftView");
			$whug["sid"]=$this->sid;
			$whug["eid"]=$eid;
			if($gift_type=="item"){
				$GTP = C("GFT_TYPE");
				$whug["gift_type"]=$GTP[$gift_type]["id"];
				$whug["address"]=array('exp','IS NOT NULL');
			}
			$UG->wharr=$whug;
			$UG->send_address_download();
		}
}